<template>
  <div class="zd-address">
    <!-- 查询区域 -->
    <el-form ref="form" :model="searchData" label-width="120px" class="clearfix">
      <el-form-item label="地点编号" class="pull-left">
        <el-input v-model="searchData.placeCode" placeholder="请输入地点编号">
        </el-input>
      </el-form-item>
      <el-form-item label="地点名称" class="pull-left">
        <el-input v-model="searchData.placeName" placeholder="请输入地点名称">
        </el-input>
      </el-form-item>
      <!-- 操作区域 -->
      <div class="pull-right">
        <el-button @click="searchHandler" class=" pd-bt">查询</el-button>
        <el-button type="primary" @click="queryHandler" class=" pd-bt">添加</el-button>
      </div>
    </el-form>

    <!---->
    <template>
      <el-table  :data="tableData" border empty-text="当前列表为空" v-loading="loading" element-loading-text="加载中" style="width: 100%">
        <!-- <el-table-column align="center" label="序号" width="70">
          <template slot-scope="scope">
            {{scope.$index+1}}
          </template>
        </el-table-column> -->
        <el-table-column prop="Code" align="center" label="地点编号" min-width="300">
        </el-table-column>
        <el-table-column prop="Name" label="地点名称" align="center" min-width="300">
        </el-table-column>

        <el-table-column min-width="100" align="center" label="操作">
          <template slot-scope="scope">
            <el-button size="small" type="text" @click="queryHandler(scope.$index, scope.row)">查看</el-button>
            <el-button size="small" type="text" @click="deleteHandler(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <!--分页-->
    <div class="block mg-20-auto pagination-fixed">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="pageCount" :page-sizes="[10, 20, 30, 40]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

    <!-- 编辑查看对话框 -->
    <el-dialog :title="dialogTitle" :close-on-click-modal="false"  v-loading="saveloading"
               element-loading-text="正在保存"  :visible.sync="centerDialogVisible" width="50%">

      <el-form :model="form" :rules="formRules" ref="addressForm">

        <el-form-item label="地点编号" :label-width="formLabelWidth" prop="Code">
          <el-input v-model="form.Code" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="地点名称" :label-width="formLabelWidth" prop="Name">
          <el-input v-model="form.Name" auto-complete="off"></el-input>
        </el-form-item>

      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveHandler">保 存</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "address1",
  data() {
    return {
      searchData: {
        placeCode: "",
        placeName: ""
      },
      dialogTitle: "",
      form: {
        Name: "",
        Code: ""
      },
      formRules: {
        Name: [{ required: true, message: "请填写地点名称", trigger: "blur" }],
        Code: [{ required: true, message: "请填写地点编号", trigger: "blur" }]
      },
      centerDialogVisible: false,
      formLabelWidth: "120px",
      total: null,
      pageCount: 0,
      pageSize: 10,
      loading: false,
      saveloading: false,
      tableData: [],
      maxHeight:''
    };
  },
beforeMount:function() {
  let that=this;
  let appHeight=$(window).height();
  let autoheight=(appHeight-260)+'px'
  that.maxHeight=autoheight
},
  mounted() {},
  methods: {
    queryHandler(index, row) {
      if (row) {
        this.dialogTitle = "查看招募地点";
        this.form = JSON.parse(JSON.stringify(row));
      } else {
        this.dialogTitle = "添加招募地点";
        this.form = {
          Name: "",
          Code: ""
        };
        this.$nextTick(() => {
          this.$refs.addressForm.resetFields();
        });
      }
      this.centerDialogVisible = true;
    },
    deleteHandler(index, row) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          // this.tableData.splice(index, 1);

          let that = this;
          that
            .service({
              url: "Dic/DeleteRecruitPlace?sysId=" + row.SYS_ID,
              method: "post"
            })
            .then(resp => {
              this.$message({
                type: "success",
                message: "删除成功!"
              });

              that.getList();
              // that.tableData.push(that.form);
            })
            .catch(data => {
              that.loading = false;
            });
        })
        .catch(() => {});
    },
    handleSizeChange(val) {
      //条数改变
      this.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      //页数改变
      this.pageCount = val;
      this.getList();
    },
    getList() {
      let that = this;
      this.loading = true;
      that
        .service({
          url: "Dic/GetRecruitPlace",
          method: "get",
          params: {
            ...that.searchData,
            pageSize: that.pageSize,
            pageCount: that.pageCount - 1
          }
        })
        .then(resp => {
          that.tableData = resp.dtData;
          that.total = parseInt(resp.dtData[0].TotalCount);
          that.loading = false;
        })
        .catch(data => {
          that.loading = false;
        });
    },
    searchHandler() {
      this.pageCount = 1; // 分页索引
      this.getList();
    },
    saveHandler() {
      let that = this;
      this.$refs.addressForm.validate(valid => {
        if (valid) {
          that.saveloading = true;

          that
            .service({
              url: "Dic/SaveRecruitPlace",
              method: "post",
              data: {
                ...that.form
              }
            })
            .then(resp => {
              that.getList();
              // that.tableData.push(that.form);
              that.centerDialogVisible = false;
              that.saveloading = false;
            })
            .catch(data => {
              that.getList();
              that.loading = false;
              that.saveloading = false;
            });
        }
      });
    }
  },
  mounted() {
    this.getList();
  },
  components: {}
};
</script>
<style lang="scss">
.zd-address {
}

.zd-address .el-dialog__wrapper .el-dialog .el-dialog__footer {
  text-align: center;
}
</style>